namespace DotNetNuke.UI.Skins
{
    using DotNetNuke.Common;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Entities.Users;
    using DotNetNuke.Framework;
    using DotNetNuke.Services.Exceptions;
    using DotNetNuke.Services.Localization;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Web.UI.WebControls;

    public class SkinControl : UserControlBase
    {
        private static List<WeakReference> __ENCList = new List<WeakReference>();
        //("cboSkin")]
        private DropDownList _cboSkin;
        //("cmdPreview")]
        private LinkButton _cmdPreview;
        private string _localResourceFile;
        private PortalInfo _objPortal;
        //("optHost")]
        private RadioButton _optHost;
        //("optSite")]
        private RadioButton _optSite;
        private string _SkinRoot;
        private string _SkinSrc;
        private string _Width;

        public SkinControl()
        {
            base.Load += new EventHandler(this.Page_Load);
            base.Init += new EventHandler(this.Page_Init);
            List<WeakReference> refObjectHelperL0 = __ENCList;
            lock (refObjectHelperL0)
            {
                __ENCList.Add(new WeakReference(this));
            }
            this._Width = "";
        }

        private void cmdPreview_Click(object sender, EventArgs e)
        {
            if (this.SkinSrc != "")
            {
                string strType = this.SkinRoot.Substring(0, this.SkinRoot.Length - 1);
                string strURL = DotNetNuke.Common.Globals.AddHTTP(DotNetNuke.Common.Globals.GetDomainName(this.Request)) + DotNetNuke.Common.Globals.ApplicationURL(this._objPortal.HomeTabId).Replace("~", "");
                if (strURL.IndexOf("?") > 0)
                {
                    strURL = strURL + "&";
                }
                else
                {
                    strURL = strURL + "?";
                }
                strURL = strURL + "portalid=" + Conversions.ToString(this._objPortal.PortalID) + "&" + strType + "Src=" + DotNetNuke.Common.Globals.QueryStringEncode(this.SkinSrc.Replace(".ascx", ""));
                if ((this.SkinRoot == SkinInfo.RootContainer) && (this.Request.QueryString["ModuleId"] != null))
                {
                    strURL = strURL + "&ModuleId=" + this.Request.QueryString["ModuleId"].ToString();
                }
                this.Response.Write("<script>window.open('" + strURL + "','_blank')</script>");
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// format skin name
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="strSkinFolder">The Folder Name</param>
        /// <param name="strSkinFile">The File Name without extension</param>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        private string FormatSkinName(string strSkinFolder, string strSkinFile)
        {
            if (strSkinFolder.ToLower() == "_default")
            {
                return strSkinFile;
            }
            string refStringHelperL0 = strSkinFile.ToLower();
            if ((refStringHelperL0 == "skin") || (refStringHelperL0 == "container"))
            {
            }
            if (((refStringHelperL0 == "default") ? 1 : 0) != 0)
            {
                return strSkinFolder;
            }
            return (strSkinFolder + " - " + strSkinFile);
        }

        [DebuggerStepThrough]
        private void InitializeComponent()
        {
        }

        private void LoadSkins()
        {
            string strLastFolder;
            string strRoot;
            string strSeparator = "----------------------------------------";
            this.cboSkin.Items.Clear();
            if (this.optHost.Checked)
            {
                strLastFolder = "";
                strRoot = DotNetNuke.Common.Globals.HostMapPath + this.SkinRoot;
                if (Directory.Exists(strRoot))
                {
                    //foreach (string strFolder in Directory.GetDirectories(strRoot))
                    //{
                    //    if (!strFolder.EndsWith("_default"))
                    //    {
                    //        foreach (string strFile in Directory.GetFiles(strFolder, "*.ascx"))
                    //        {
                    //            strFolder = Strings.Mid(strFolder, Strings.InStrRev(strFolder, @"\", -1, CompareMethod.Binary) + 1);
                    //            if (strLastFolder != strFolder)
                    //            {
                    //                if (strLastFolder != "")
                    //                {
                    //                    this.cboSkin.Items.Add(new ListItem(strSeparator, ""));
                    //                }
                    //                strLastFolder = strFolder;
                    //            }
                    //            this.cboSkin.Items.Add(new ListItem(this.FormatSkinName(strFolder, Path.GetFileNameWithoutExtension(strFile)), "[G]" + this.SkinRoot + "/" + strFolder + "/" + Path.GetFileName(strFile)));
                    //        }
                    //    }
                    //}

                    String[] dirs = Directory.GetDirectories(strRoot);
                    for (int i = 0; i < dirs.Length; ++i)
                    {
                        string strFolder = dirs[i];
                        if (!strFolder.EndsWith("_default"))
                        {
                            foreach (string strFile in Directory.GetFiles(strFolder, "*.ascx"))
                            {
                                strFolder = Strings.Mid(strFolder, Strings.InStrRev(strFolder, @"\", -1, CompareMethod.Binary) + 1);
                                if (strLastFolder != strFolder)
                                {
                                    if (strLastFolder != "")
                                    {
                                        this.cboSkin.Items.Add(new ListItem(strSeparator, ""));
                                    }
                                    strLastFolder = strFolder;
                                }
                                this.cboSkin.Items.Add(new ListItem(this.FormatSkinName(strFolder, Path.GetFileNameWithoutExtension(strFile)), "[G]" + this.SkinRoot + "/" + strFolder + "/" + Path.GetFileName(strFile)));
                            }
                        }
                    }

                }
            }
            if (this.optSite.Checked)
            {
                strLastFolder = "";
                strRoot = this._objPortal.HomeDirectoryMapPath + this.SkinRoot;
                if (Directory.Exists(strRoot))
                {
                    string[] dirs = Directory.GetDirectories(strRoot);
                    for (int i = 0; i < dirs.Length; ++i)
                    {
                        string strFolder = dirs[i];
                        foreach (string strFile in Directory.GetFiles(strFolder, "*.ascx"))
                        {
                            strFolder = Strings.Mid(strFolder, Strings.InStrRev(strFolder, @"\", -1, CompareMethod.Binary) + 1);
                            if (strLastFolder != strFolder)
                            {
                                if (strLastFolder != "")
                                {
                                    this.cboSkin.Items.Add(new ListItem(strSeparator, ""));
                                }
                                strLastFolder = strFolder;
                            }
                            this.cboSkin.Items.Add(new ListItem(this.FormatSkinName(strFolder, Path.GetFileNameWithoutExtension(strFile)), "[L]" + this.SkinRoot + "/" + strFolder + "/" + Path.GetFileName(strFile)));
                        }
                    }
                }
            }
            if (this.cboSkin.Items.Count > 0)
            {
                this.cboSkin.Items.Insert(0, new ListItem(strSeparator, ""));
            }
            this.cboSkin.Items.Insert(0, new ListItem("<" + DotNetNuke.Services.Localization.Localization.GetString("Not_Specified") + ">", ""));
            int refIntHelperL4 = this.cboSkin.Items.Count - 1;
            for (int intIndex = 0; intIndex <= refIntHelperL4; intIndex++)
            {
                if (this.cboSkin.Items[intIndex].Value.ToLower() == Convert.ToString(RuntimeHelpers.GetObjectValue(this.ViewState["SkinSrc"])).ToLower())
                {
                    this.cboSkin.Items[intIndex].Selected = true;
                    break;
                }
            }
        }

        private void optHost_CheckedChanged(object sender, EventArgs e)
        {
            this.LoadSkins();
        }

        private void optSite_CheckedChanged(object sender, EventArgs e)
        {
            this.LoadSkins();
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this.InitializeComponent();
        }

        private void Page_Load(object sender, EventArgs e)
        {
            try
            {
                PortalController objPortals = new PortalController();
                if ((this.Request.QueryString["pid"] != null) & ((this.PortalSettings.ActiveTab.ParentId == this.PortalSettings.SuperTabId) | UserController.GetCurrentUserInfo().IsSuperUser))
                {
                    this._objPortal = objPortals.GetPortal(int.Parse(this.Request.QueryString["pid"]));
                }
                else
                {
                    this._objPortal = objPortals.GetPortal(this.PortalSettings.PortalId);
                }
                if (!this.Page.IsPostBack)
                {
                    this.ViewState["SkinControlWidth"] = this._Width;
                    this.ViewState["SkinRoot"] = this._SkinRoot;
                    this.ViewState["SkinSrc"] = this._SkinSrc;
                    if (this._Width != "")
                    {
                        this.cboSkin.Width = Unit.Parse(this._Width);
                    }
                    if (this._SkinSrc != "")
                    {
                        switch (this._SkinSrc.Substring(0, 3))
                        {
                            case "[L]":
                                this.optHost.Checked = false;
                                this.optSite.Checked = true;
                                break;

                            case "[G]":
                                this.optSite.Checked = false;
                                this.optHost.Checked = true;
                                break;
                        }
                    }
                    this.LoadSkins();
                }
            }
            catch (Exception exception1)
            {
                
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
                
            }
        }

        protected virtual DropDownList cboSkin
        {
           
            get
            {
                return this._cboSkin;
            }
            
            set
            {
                this._cboSkin = value;
            }
        }

        protected virtual LinkButton cmdPreview
        {
           
            get
            {
                return this._cmdPreview;
            }
            
            set
            {
                EventHandler handler = new EventHandler(this.cmdPreview_Click);
                if (this._cmdPreview != null)
                {
                    this._cmdPreview.Click -= handler;
                }
                this._cmdPreview = value;
                if (this._cmdPreview != null)
                {
                    this._cmdPreview.Click += handler;
                }
            }
        }

        public string LocalResourceFile
        {
            get
            {
                if (this._localResourceFile == "")
                {
                    return (this.TemplateSourceDirectory + "/App_LocalResources/SkinControl.ascx");
                }
                return this._localResourceFile;
            }
            set
            {
                this._localResourceFile = value;
            }
        }

        protected virtual RadioButton optHost
        {
           
            get
            {
                return this._optHost;
            }
            
            set
            {
                EventHandler handler = new EventHandler(this.optHost_CheckedChanged);
                if (this._optHost != null)
                {
                    this._optHost.CheckedChanged -= handler;
                }
                this._optHost = value;
                if (this._optHost != null)
                {
                    this._optHost.CheckedChanged += handler;
                }
            }
        }

        protected virtual RadioButton optSite
        {
           
            get
            {
                return this._optSite;
            }
            
            set
            {
                EventHandler handler = new EventHandler(this.optSite_CheckedChanged);
                if (this._optSite != null)
                {
                    this._optSite.CheckedChanged -= handler;
                }
                this._optSite = value;
                if (this._optSite != null)
                {
                    this._optSite.CheckedChanged += handler;
                }
            }
        }

        public string SkinRoot
        {
            get
            {
                return Convert.ToString(RuntimeHelpers.GetObjectValue(this.ViewState["SkinRoot"]));
            }
            set
            {
                this._SkinRoot = value;
            }
        }

        public string SkinSrc
        {
            get
            {
                if (this.cboSkin.SelectedItem != null)
                {
                    return this.cboSkin.SelectedItem.Value;
                }
                return "";
            }
            set
            {
                this._SkinSrc = value;
            }
        }

        public string Width
        {
            get
            {
                return Convert.ToString(RuntimeHelpers.GetObjectValue(this.ViewState["SkinControlWidth"]));
            }
            set
            {
                this._Width = value;
            }
        }
    }
}

